<template>
  <div class="bigbox">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="上级分类">
        <el-select v-model="form.region" placeholder="请选择分类">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="item in getTopCategory"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="分类图片">
        <el-upload
          class="avatar-uploader"
          action="http://kumanxuan1.f3322.net:8360/admin/upload/goodNewPic"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="headers"
          name="good_pic"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="显示">
        <el-switch v-model="form.show"></el-switch>
      </el-form-item>

      <el-form-item label="排序">
        <el-input v-model="form.paixu"></el-input>
      </el-form-item>

      <el-form-item label="分类描述">
        <el-input type="textarea" v-model="form.miaoshu"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addFn">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addCategory, getTopCategory } from "../../request/httpApi";
export default {
  data() {
    return {
      form: {
        name: "",
        region: "",
        show: false,
        paixu: 0,
        miaoshu: "",
      },
      imageUrl: "",
      getTopCategory: [],

      front_desc: "",
      icon_url: "",
      parent_id: 0,
      show_index: 2,
      type: 0,
      banner_url:'',
      img_url: "",
      is_new: false,
       headers:{
        'X-Nideshop-Token':localStorage.getItem('token')
      }
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      this.imageUrl = file.response.data.fileUrl
      console.log(this.imageUrl);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 保存
    addFn() {
      addCategory({
        banner_url: this.banner_url,
        front_desc: this.front_desc,
        icon_url: this.icon_url,
        parent_id: this.parent_id,
        show_index: this.show_index,
        type: this.type,
        img_url: this.img_url,
        is_new: this.is_new,
        name: this.form.name, // 名称
        wap_banner_url: this.imageUrl, // 图片
        is_show: this.form.show, // 显示隐藏
        sort_order: this.form.paixu, // 排序
        front_name: this.form.miaoshu, // 描述
      }).then((res) => {
        console.log(res);
      });
    },
  },
  created() {
    getTopCategory().then((res) => {
      console.log(res.data);
      this.getTopCategory = res.data;
    });
  },
};
</script>

<style lang="less" scoped>
.bigbox {
  background: #fff;
  .el-form {
    padding-top: 20px;
    padding-left: 40px;
  }
  /deep/ .el-input__inner {
    width: 260px;
  }
  /deep/ .el-textarea__inner {
    width: 260px;
  }
  /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  /deep/ .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  /deep/ .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
